//封装一个指令，搜索框自动聚焦
const directiveObj = {
  install(Vue) {
    Vue.directive("fofo", {
      //插入到真实dom时触发一次
      inserted(el) {
        if (el.nodeName === "INPUT" || el.nodeName === "TEXTAREA") {
          el.focus();
        } else {
          const theInput = el.querySelector("input");
          const theTextArea = el.querySelector("textarea");
          if (theInput) {
            theInput.focus();
          }
          if (theTextArea) {
            theTextArea.focus();
          }
        }
      },
      //指令所在标签，被更新时触发
      update(el) {
        if (el.nodeName === "INPUT" || el.nodeName === "TEXTAREA") {
          el.focus();
        } else {
          const theInput = el.querySelector("input");
          const theTextArea = el.querySelector("textarea");
          if (theInput) {
            theInput.focus();
          }
          if (theTextArea) {
            theTextArea.focus();
          }
        }
      },
    });
  },
};

export default directiveObj;
